package com.lxu.report.service;

import com.lxu.report.entity.YwReportData;
import com.lxu.report.entity.enums.ReportEnum;
import com.lxu.report.util.CommonUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;
import java.util.List;

@Service
public class ReportFileOperationService {


    @Transactional
    public void refreshFileAndDB(String absoluteFile, YwReportData ywReportData, boolean isWord) {
        absoluteFile = CommonUtils.formatFileName(absoluteFile);
        String templateTypeCode = ywReportData.getTemplateTypeCode();
        String exportRecordId = ywReportData.getExportRecordId();
        if(StringUtils.isBlank(templateTypeCode)){
            throw new RuntimeException("模板类型编号(templateTypeCode)不能为空");
        }
        if(StringUtils.isBlank(exportRecordId)){
            throw new RuntimeException("编号(id)不能为空");
        }
        String orginalFile ="";
        switch (ReportEnum.TEMPLATE_TYPE.valueOf(templateTypeCode)){
            case LXBG:
                break;

        }
    }


    /**
     * 删除临时生成的各种文件
     * @param srcfiles
     */
    public void deleteSrcfiles(List<File> srcfiles) {
        if(CollectionUtils.isEmpty(srcfiles)){
            return;
        }
        srcfiles.forEach(srcfile ->{
            delFile(srcfile);
        });
    }

    /**
     * 删除本地历史文件
     * @param orginalFile 文件全路径
     */
    private void deleteOriginalFile(String orginalFile) {
        if(StringUtils.isBlank(orginalFile)){
            return;
        }
        String baseDir = "e:\\\\files\\\\";
        String temp = orginalFile.substring(0,orginalFile.lastIndexOf('/'));
        int day = Integer.parseInt(temp.substring(temp.lastIndexOf('/')+1));
        if(LocalDate.now().getDayOfMonth() ==day){
            delFile(new File(baseDir+CommonUtils.getFilePath(orginalFile)));
        }
    }

    /**
     * 文件删除
     * @param file
     */
    private void delFile(File file){
        if(file.exists() && file.isFile()){
            file.delete();
        }
    }
}
